package com.firegod.group.service.impl;

import com.firegod.commonutils.result.ResultCode;
import com.firegod.group.form.TopicMessageForm;
import com.firegod.group.form.TopicMessageReplyForm;
import com.firegod.group.service.CommentService;
import com.firegod.servicebase.dao.CommentRepository;
import com.firegod.servicebase.dao.TopicRepository;
import com.firegod.servicebase.dao.UserRepository;
import com.firegod.servicebase.entity.Comment;
import com.firegod.servicebase.entity.Topic;
import com.firegod.servicebase.entity.User;
import com.firegod.servicebase.exceptionhandler.exception.GuliException;
import com.firegod.shiro.service.TokenService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TopicRepository topicRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public String createComment(String topicId, String topicMessage) {
        String userId = tokenService.getUserId();
        if (topicId == null) {
            throw GuliException.from("话题不存在！");
        }
        Topic topic = topicRepository.getTopicByTopicId(topicId);
        if (topic == null) {
            throw GuliException.from("话题不存在！");
        }
        if (topicMessage == null) {
            throw GuliException.from("内容不能为空！");
        }
        Comment comment = new Comment();
        comment.setUser(userRepository.getByUserId(userId));
        comment.setContent(topicMessage);
        comment.setToTopic(topic);
        comment.setDate(new Date());

        //commentId
        String commentId = RandomStringUtils.randomAlphanumeric(16);
        while (commentRepository.getCommentByCommentId(commentId) != null) {
            commentId = RandomStringUtils.randomAlphanumeric(16);
        }
        comment.setCommentId(commentId);
        commentRepository.saveAndFlush(comment);

        return comment.getCommentId();
    }

    @Override
    public String createReply(String replyTopicMessage, String replyToUserId,
                              String replyToUserName, String topicMessageId, String topicId) {
        Comment comment = new Comment();
        //TODO 内容需要从前端获取，name无需从前端获取
        comment.setContent(replyTopicMessage);

        String userId = tokenService.getUserId();
        comment.setUser(userRepository.getByUserId(userId));
        User user = userRepository.getByUserId(replyToUserId);

        if (user != null) {
            comment.setToUser(user);
        } else {
            throw GuliException.from("toUserId 不能为空");
        }
        comment.setDate(new Date());

        //commentId
        String commentId = RandomStringUtils.randomAlphanumeric(16);
        while (commentRepository.getCommentByCommentId(commentId) != null) {
            commentId = RandomStringUtils.randomAlphanumeric(16);
        }
        comment.setCommentId(commentId);

        Topic topic = topicRepository.getTopicByTopicId(topicId);
        if (topic == null)
            throw GuliException.from(ResultCode.TOPIC_NOT_EXIST);
        comment.setToTopic(topic);

        //二级评论列表
        Comment toComment = commentRepository.getCommentByCommentId(topicMessageId);
        comment.setFatherComment(toComment);
//        if (toComment == null)
//            throw GuliException.from(ResultCode.NO_COMMENT_ID);
//        System.out.println("二级评论列表" + toComment);
//        System.out.println(topicMessageId);
//
//        toComment.getCommentList().add(comment);
        commentRepository.saveAndFlush(comment);
        return comment.getCommentId();
    }

    @Override
    public List<TopicMessageForm> getTopicComment(String topicId) {
        Topic topic = topicRepository.getTopicByTopicId(topicId);
        if (topic == null) {
            throw GuliException.from(ResultCode.TOPIC_NOT_EXIST);
        }
        List<Comment> comments = topic.getTopicCommentList();
        List<TopicMessageForm> topicMessageForms = new ArrayList<>();

        for (var comment : comments) {
            if (comment.getToUser() != null)
                continue;
            TopicMessageForm topicMessageForm = new TopicMessageForm();
            topicMessageForm.setUserId(comment.getUser().getUserId());
            topicMessageForm.setUserName(comment.getUser().getName());
            topicMessageForm.setUserImg("");
            topicMessageForm.setTopicMessageId(comment.getCommentId());
            topicMessageForm.setTopicMessageDate(comment.getDate());
            topicMessageForm.setTopicMessageContent(comment.getContent());

            topicMessageForms.add(topicMessageForm);
        }
        return topicMessageForms;
    }

    @Override
    public List<TopicMessageReplyForm> getReplyComment(String topicId) {
        Topic topic = topicRepository.getTopicByTopicId(topicId);
        if (topic == null)
            throw GuliException.from(ResultCode.TOPIC_NOT_EXIST);
        List<Comment> comments = topic.getTopicCommentList();
        List<TopicMessageReplyForm> topicMessageReplyForms = new ArrayList<>();

        for (var comment : comments) {
            var comList = comment.getCommentList();
            for (var com : comList) {
                TopicMessageReplyForm topicMessageReplyForm = new TopicMessageReplyForm();
                topicMessageReplyForm.setUserId(com.getUser().getUserId());
                topicMessageReplyForm.setUserName(com.getUser().getName());
                topicMessageReplyForm.setTopicMessageId(com.getCommentId());

                topicMessageReplyForm.setToUserId(comment.getUser().getUserId());
                topicMessageReplyForm.setToUserName(comment.getUser().getName());

                topicMessageReplyForm.setTopicMessageReplyId(comment.getCommentId());

                topicMessageReplyForm.setTopicMessageReplyDate(com.getDate());
                topicMessageReplyForm.setReplyContent(com.getContent());

                topicMessageReplyForms.add(topicMessageReplyForm);
            }
        }
        return topicMessageReplyForms;

    }

    @Override
    public void deleteComment(String commentId) {
        if (commentId == null)
            throw GuliException.from(ResultCode.NO_COMMENT_ID);
        Comment comment = commentRepository.getCommentByCommentId(commentId);
        if (comment == null)
            throw GuliException.from(ResultCode.COMMENT_NOT_EXIST);

        if (!comment.getUser().getUserId().equalsIgnoreCase(tokenService.getUserId())) {
            throw GuliException.from("您没有删除的权限！");
        }

//        List<Comment> commentList = comment.getCommentList();
//        for (var com : commentList) {
//            commentRepository.delete(com);
//        }
//        commentRepository.flush();
        commentRepository.delete(comment);
        commentRepository.flush();
    }
}
